package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.dao.SysUserHospitalMapper;
import net.zigin.nhi.could.waste.dto.InOrOutDto;
import net.zigin.nhi.could.waste.dto.QrCodeDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.model.WasteBase;
import net.zigin.nhi.could.waste.util.EncryptDataUtil;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.vo.WasteBaseRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.WasteBaseRecordMapper;
import net.zigin.nhi.could.waste.model.WasteBaseRecord;
import net.zigin.nhi.could.waste.service.WasteBaseRecordService;

import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-13 21:14:44                                               
##################################################################*/


/**
 * WasteRecord服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: WasteRecordServiceImpl
 * @date 2021-9-13 21:14:44
 */
@Service
public class WasteBaseRecordServiceImpl extends AbstractServiceImpl<WasteBaseRecord, String> implements WasteBaseRecordService {
    @Autowired
    private WasteBaseRecordMapper wasteBaseRecordMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;

    @Override
    public BaseMapper<WasteBaseRecord, String> getMapper() {
        return wasteBaseRecordMapper;
    }

    @Override
    public void generateRecord(WasteBase wasteBase, String remark, Float weight) {
        // todo content 包含流转信息
        WasteBaseRecord wasteBaseRecord = buildRecord(wasteBase);
        wasteBaseRecord.setWeight(weight);
        wasteBaseRecordMapper.insert(wasteBaseRecord);
    }

    @Override
    public void generateRecord(WasteBase wasteBase, String remark) {
        WasteBaseRecord wasteBaseRecord = buildRecord(wasteBase);
        wasteBaseRecordMapper.insert(wasteBaseRecord);
    }

    private WasteBaseRecord buildRecord(WasteBase wasteBase) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String staffId = sysUserHospitalMapper.getIdByUserId(tokenInfo.getUserId());
        WasteBaseRecord wasteBaseRecord = new WasteBaseRecord(wasteBase, staffId);
        String content = "";
        if (!StringUtil.isNullOrEmpty(wasteBase.getWasteBoxId())) {
            content = "随箱：" + wasteBase.getWasteBoxCode() + ";";
        }
        if (wasteBase.getStatus().equals("do_collect")) {
            content = content + wasteBase.getHospitalDepartName() + "," + wasteBase.getCollectUserStaffName() + "收集";
        } else if (wasteBase.getStatus().equals("in_depot")) {
            content = content + (wasteBase.getHospitalPlaceName() == null ? "" : wasteBase.getHospitalPlaceName()) + ",入库";
        } else if (wasteBase.getStatus().equals("out_depot")) {
            content = content + (wasteBase.getHospitalPlaceName() == null ? "" : wasteBase.getHospitalPlaceName()) + ",出库";
        }
        wasteBaseRecord.setContent(content);
        return wasteBaseRecord;
    }

    @Override
    public List<WasteBaseRecordVo> getListByWasteBaseId(String wasteBaseId) {
        List<WasteBaseRecord> list = wasteBaseRecordMapper.getListByWasteBaseId(wasteBaseId);
        return ReflectUtil.copyProperties(list, WasteBaseRecordVo.class);
    }

    @Override
    public List<WasteBaseRecordVo> getListByWasteBaseQrCode(QrCodeDto qrCodeDto) {
        EncryptDataUtil.EncryptData qrCode = EncryptDataUtil.decryptData(qrCodeDto.getContent());
        if (!qrCode.getType().equals("wasteBase")) {
            throw new EpiCloudException("请扫医废二维码！");
        }
        List<WasteBaseRecord> list = wasteBaseRecordMapper.getListByWasteBaseId(qrCode.getData());
        return ReflectUtil.copyProperties(list, WasteBaseRecordVo.class);
    }

    @Override
    public List<WasteBaseRecordVo> getInOrOutList(InOrOutDto inOrOutDto) {
        Map<String, Object> paramMap = getInOrListParam(inOrOutDto);
        return wasteBaseRecordMapper.inOrOutList(paramMap);
    }

    @Override
    public Integer getInOrOutCount(InOrOutDto inOrOutDto) {
        Map<String, Object> paramMap = getInOrListParam(inOrOutDto);
        return wasteBaseRecordMapper.inOrOutCount(paramMap);
    }

    private Map<String, Object> getInOrListParam(InOrOutDto inOrOutDto) {
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        String hospitalBaseId = sysUserHospitalMapper.getHospitalBaseIdBySysUserId(tokenInfo.getUserId());
        return ParamsMapUtil.getInstance()
                .addParam("hospitalBaseId", hospitalBaseId)
                .addParam("model", inOrOutDto.getModel());
        // .addParam("pageIndex", inOrOutDto.getPageIndex())
        //                .addParam("pageSize", inOrOutDto.getPageSize())
    }
}